package cn.org.qary.controller.yaokaiing.back;

import cn.org.qary.entity.OrderRecord;
import cn.org.qary.entity.Product;
import cn.org.qary.service.trait.abelianymph.OrderRecordService;
import cn.org.qary.service.trait.abelianymph.ProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.vavr.Tuple2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@Controller("back_OrderRecordController")
@RequestMapping("back_orderRecord")
public class OrderRecordController {
    @Autowired
    OrderRecordService ors;
    @Autowired
    ProductService ps;

    int rows=3;
    @RequestMapping("show/{id}/{page}")
    public String findAll(@PathVariable String id,@PathVariable(required = false) Integer page,Model model){
        if (page==null) page=1;
        Page<Object> pages = PageHelper.startPage(page, rows);

        Example condition=new Example(OrderRecord.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("orderId", id);
        List<OrderRecord> list = ors.selectByCondition(condition);

        List<Tuple2<OrderRecord, Product>> aList = new ArrayList<>();
        for (OrderRecord or : list) {
            Product product = ps.selectById(or.getProductId());
            Tuple2<OrderRecord, Product> tempTuple2 = new Tuple2<>(or, product);
            aList.add(tempTuple2);
        }

        model.addAttribute("OrderRecordProducts",aList);
        model.addAttribute("pages",pages);
        return "manage/order-view";
    }


}
